﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Om.Ptz;
using System.Windows.Controls;
using Om.Core.Singleton;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;

namespace OptoMeasure.Views;

public partial class PtzViewModel : ObservableObject
{
    private  Ptz _ptz => DevicesManager.Instance.Ptz;

    public PtzViewModel()
    {
        _ptz.Client.Connected += Connected;
    }

    private async Task Connected(ISerialPortClient client, ConnectedEventArgs e)
    {
        await ReadPtzData();
    }



    /// <summary>
    /// 当前水平角度
    /// </summary>
    [ObservableProperty] private double _currentHorizontalAngle;
    /// <summary>
    /// 当前垂直角度
    /// </summary>
    [ObservableProperty] private double _currentVerticalAngle;
    /// <summary>
    /// 设定水平角度
    /// </summary>
    [ObservableProperty] private double _setHorizontalAngle = 0;
    /// <summary>
    /// 设定垂直角度
    /// </summary>
    [ObservableProperty] private double _setVerticalAngle = 0;
    /// <summary>
    /// 水平速度
    /// </summary>
    [ObservableProperty] private int _horizontalSpeed = 30;
    /// <summary>
    /// 垂直速度
    /// </summary>
    [ObservableProperty] private int _verticalSpeed = 30;


    /// <summary>
    /// 移动到目标角度
    /// </summary>
    /// <returns></returns>
    [RelayCommand]
    private async Task HorizontalMoveTo()
    {
        await _ptz.MoveTo(EnumOrientation.Horizontal, SetHorizontalAngle);
    }
    /// <summary>
    /// 移动到目标角度
    /// </summary>
    /// <returns></returns>
    [RelayCommand]
    private async Task VerticalMoveTo()
    {
        await _ptz.MoveTo(EnumOrientation.Vertical, SetVerticalAngle);
    }

    public async Task Jog(EnumJogDirection jogDirection)
    {
        await _ptz.Jog(jogDirection);
    }

    [RelayCommand]
    public async Task Stop()
    {
        await _ptz.Stop();
    }


    [RelayCommand]
    public async Task ReadPtzData()
    {
        HorizontalSpeed = await _ptz.ReadSpeed(EnumOrientation.Horizontal);
        VerticalSpeed = await _ptz.ReadSpeed(EnumOrientation.Vertical);
        CurrentHorizontalAngle = await _ptz.ReadAngleDirectly(EnumOrientation.Horizontal);
        CurrentVerticalAngle = await _ptz.ReadAngleDirectly(EnumOrientation.Vertical);
    }

    async partial void OnHorizontalSpeedChanged(int value)
    {
        await _ptz.SetSpeed(EnumOrientation.Horizontal, value);
    }
    async partial void OnVerticalSpeedChanged(int value)
    {
        await _ptz.SetSpeed(EnumOrientation.Vertical, value);
    }
}